* Project      :  The Full, Persistent, and Symmetric Pass-Through of a Temporary VAT Cut
* Authors      :  Márcia Silva-Pereira, João Quelhas, Tiago Bernardino, Ricardo Duque Gabriel
* Date         :  01/04/2025
* Description  :  Event-study Regression - Heterogeneity
* Dependencies :
* Modifications: (add date, author and change)

*********************************************************
*                Event-study Regression                 *
*********************************************************

local vce_cluster ${vce_cluster}
local weights ${weights}
local variable ${variable}
local aggregation ${aggregation}

*********************************************************

* FIGURE 7: Heterogeneous Treatment Effects Analysis
*********************************************************

// Load the cleaned dataset.
use "${path_work}/auxiliar/imputation_0_only_food.dta", clear

collapse (${aggregation}) ${variable} (first) ecoicop4 treatment weight, by (id_sm five_day_number)

// Compute a price index with base in the day before policy announcement I.
local   base_date 22
egen    base_price_23032023 = total(${variable}/ (five_day_number == `base_date')), by(id_sm)
gen     index_23032023 = (${variable}/ base_price_23032023) * 100

xtset id_sm five_day_number

// Specify a list of dataset names
local coicops "0111 0112 0113 0114 0115 0116 0117"

file open  myfile using "${results_figures}/temp_file_implementation.txt", write text append
file write myfile "ecoicops" _n _n
file close myfile

file open  myfile using "${results_figures}/temp_file_reversal.txt", write text append
file write myfile "ecoicops" _n _n
file close myfile

foreach coicop in `coicops' {

preserve

drop if (treatment == 1 & ecoicop4 != `coicop')

// Keep the data around the event.
keep if five_day_number >= 17
keep if five_day_number <= 32

// Policy variable: 1 after the reference date.
replace treatment = 0 if five_day_number < 23

if ${weights} == 0 {

// Run the event-study regression.
xtevent index_23032023, policyvar(treatment) panelvar(id_sm) timevar(five_day_number) window(${window_low} ${window_high}) vce(cluster ${vce_cluster}) impute(nuchange) reghdfe trend(${trend}, method(gmm) saveoverlay)
	
}

if ${weights} == 1 {

// Run the event-study regression.
xtevent index_23032023 [pweight = weight], policyvar(treatment) panelvar(id_sm) timevar(five_day_number) window(${window_low} ${window_high}) vce(cluster ${vce_cluster}) impute(nuchange) reghdfe trend(${trend}, method(gmm) saveoverlay)
		
}

matrix results = r(delta)
matrix vcv = r(Vdelta)
local z = 1.96

file open  myfile using "${results_figures}/temp_file_implementation.txt", write text append

scalar coef = results[1,10]
scalar se   = sqrt(vcv[10,10])
generate coeff = coef
generate z = `z'
generate see = se
scalar lb = coeff - z * see
generate lbb = lb
scalar ub = coeff + z * see
generate ubb = ub
format %12.0g coeff lbb ubb
file write myfile "`coicop', " (lbb) ", " (coeff) ", " (ubb) _n _n
drop coeff z see lbb ubb

file close myfile

restore

preserve

drop if (treatment == 1 & ecoicop4 != `coicop')

// Keep the data around the event.
keep if five_day_number >= 60
keep if five_day_number <= 83

// Policy variable: 1 after the reference date.
replace treatment = 0 if five_day_number < 66

if ${weights} == 0 {

// Run the event-study regression.
xtevent index_23032023, policyvar(treatment) panelvar(id_sm) timevar(five_day_number) window(${window_low} 16) vce(cluster ${vce_cluster}) impute(nuchange) reghdfe trend(${trend}, method(gmm) saveoverlay)
	
}

if ${weights} == 1 {

// Run the event-study regression.
xtevent index_23032023 [pweight = weight], policyvar(treatment) panelvar(id_sm) timevar(five_day_number) window(${window_low} 16) vce(cluster ${vce_cluster}) impute(nuchange) reghdfe trend(${trend}, method(gmm) saveoverlay)
		
}

matrix results = r(delta)
matrix vcv = r(Vdelta)
local z = 1.96

file open  myfile using "${results_figures}/temp_file_reversal.txt", write text append

scalar coef = results[1,19]
scalar se   = sqrt(vcv[19,19])
generate coeff = coef
generate z = `z'
generate see = se
scalar lb = coeff - z * see
generate lbb = lb
scalar ub = coeff + z * see
generate ubb = ub
format %12.0g coeff lbb ubb
file write myfile "`coicop', "(lbb) ", " (coeff) ", " (ubb) _n _n
drop coeff z see lbb ubb

file close myfile

restore

if `coicop' == 0115 {

// Make the plot with the results.
xteventplot, ytitle("Percentage change") xtitle("5-day Periods to Event") ///
	nosupt scatterplotopts(lcolor(dknavy) recast(connected) mcolor(dknavy) ///
	msymbol(circle)) ciplotopts(recast(rarea) mcolor(black) fcolor(dknavy*0.2)) ///
	graphregion(fcolor(white)) yscale(lcolor(black) lwidth(none) line) ///
	tscale(lcolor(black) lwidth(none) line) ylabel(-40(10)10) ///
	yline(6, lcolor("dknavy") lpattern(dash) lwidth(medthin)) ///
	text(3.7 -4.8 "100% Pass-Through", color("dknavy") placement(e)) ///
	xline(0, lcolor("gray*0.5") lpattern(solid) lwidth(medthin)) ///
	text(7.5 0.1 "Announcement", color("gray") placement(e)) ///
	xline(14, lcolor("gray*0.5") lpattern(solid) lwidth(medthin)) ///
	text(7.5 11.8 "Reversal", color("gray") placement(e))

// Export the graph.
graph export "${results_figures}/figure_a9a.png", as(png) replace width(2400) height(1372)

// Make the plot with the results.
xteventplot, overlay(trend) ytitle("Percentage change") xtitle("5-day Periods to Event") ///
	scatterplotopts(lcolor(dknavy) recast(connected) mcolor(dknavy) ///
	msymbol(circle)) ciplotopts(recast(rarea) mcolor(black) fcolor(dknavy*0.2)) ///
	graphregion(fcolor(white)) yscale(lcolor(black) lwidth(none) line) ///
	tscale(lcolor(black) lwidth(none) line) ylabel(-10(10)30) ///
	yline(6, lcolor("dknavy") lpattern(dash) lwidth(medthin)) ///
	text(4.4 -4.8 "100% Pass-Through", color("dknavy") placement(e)) ///
	xline(0, lcolor("gray*0.5") lpattern(solid) lwidth(medthin)) ///
	text(27.5 0.1 "Announcement", color("gray") placement(e)) ///
	xline(14, lcolor("gray*0.5") lpattern(solid) lwidth(medthin)) ///
	text(27.5 11.8 "Reversal", color("gray") placement(e))

// Export the graph.
graph export "${results_figures}/figure_a9b.png", as(png) replace width(2400) height(1372)

}


}

*********************************************************

// Load the cleaned dataset.
use "${path_work}/auxiliar/imputation_0_only_food.dta", clear

collapse (${aggregation}) ${variable} (first) treatment weight white_label, by (id_sm five_day_number)

// Compute a price index with base in the day before policy announcement I.
local   base_date 22
egen    base_price_23032023 = total(${variable}/ (five_day_number == `base_date')), by(id_sm)
gen     index_23032023 = (${variable}/ base_price_23032023) * 100

xtset id_sm five_day_number

file open  myfile using "${results_figures}/temp_file_implementation.txt", write text append
file write myfile "white_label (trademark-labels (0) and white-labels (1))" _n _n
file close myfile

file open  myfile using "${results_figures}/temp_file_reversal.txt", write text append
file write myfile "white_label (trademark-labels (0) and white-labels (1))" _n _n
file close myfile


forval i = 0(1)1 {

preserve

keep if white_label == `i'

// Keep the data around the event.
keep if five_day_number >= 17
keep if five_day_number <= 32

// Policy variable: 1 after the reference date.
replace treatment = 0 if five_day_number < 23

if ${weights} == 0 {

// Run the event-study regression.
xtevent index_23032023, policyvar(treatment) panelvar(id_sm) timevar(five_day_number) window(${window_low} ${window_high}) vce(cluster ${vce_cluster}) impute(nuchange) reghdfe trend(${trend}, method(gmm) saveoverlay)
	
}

if ${weights} == 1 {

// Run the event-study regression.
xtevent index_23032023 [pweight = weight], policyvar(treatment) panelvar(id_sm) timevar(five_day_number) window(${window_low} ${window_high}) vce(cluster ${vce_cluster}) impute(nuchange) reghdfe trend(${trend}, method(gmm) saveoverlay)
		
}

matrix results = r(delta)
matrix vcv = r(Vdelta)
local z = 1.96

file open  myfile using "${results_figures}/temp_file_implementation.txt", write text append

scalar coef = results[1,10]
scalar se   = sqrt(vcv[10,10])
generate coeff = coef
generate z = `z'
generate see = se
scalar lb = coeff - z * see
generate lbb = lb
scalar ub = coeff + z * see
generate ubb = ub
format %12.0g coeff lbb ubb
file write myfile "`i', " (lbb) ", " (coeff) ", " (ubb) _n _n
drop coeff z see lbb ubb

file close myfile

restore

preserve

keep if white_label == `i'

// Keep the data around the event.
keep if five_day_number >= 60
keep if five_day_number <= 83

// Policy variable: 1 after the reference date.
replace treatment = 0 if five_day_number < 66

if ${weights} == 0 {

// Run the event-study regression.
xtevent index_23032023, policyvar(treatment) panelvar(id_sm) timevar(five_day_number) window(${window_low} 16) vce(cluster ${vce_cluster}) impute(nuchange) reghdfe trend(${trend}, method(gmm) saveoverlay)
	
}

if ${weights} == 1 {

// Run the event-study regression.
xtevent index_23032023 [pweight = weight], policyvar(treatment) panelvar(id_sm) timevar(five_day_number) window(${window_low} 16) vce(cluster ${vce_cluster}) impute(nuchange) reghdfe trend(${trend}, method(gmm) saveoverlay)
		
}

matrix results = r(delta)
matrix vcv = r(Vdelta)
local z = 1.96

file open  myfile using "${results_figures}/temp_file_reversal.txt", write text append

scalar coef = results[1,19]
scalar se   = sqrt(vcv[19,19])
generate coeff = coef
generate z = `z'
generate see = se
scalar lb = coeff - z * see
generate lbb = lb
scalar ub = coeff + z * see
generate ubb = ub
format %12.0g coeff lbb ubb
file write myfile "`i', " (lbb) ", " (coeff) ", " (ubb) _n _n
drop coeff z see lbb ubb

file close myfile

restore

}

*********************************************************

// Load the cleaned dataset.
use "${path_work}/auxiliar/imputation_0_only_food.dta", clear

collapse (${aggregation}) ${variable} (first)  treatment weight domestic, by (id_sm five_day_number)

// Compute a price index with base in the day before policy announcement I.
local   base_date 22
egen    base_price_23032023 = total(${variable}/ (five_day_number == `base_date')), by(id_sm)
gen     index_23032023 = (${variable}/ base_price_23032023) * 100

xtset id_sm five_day_number

file open  myfile using "${results_figures}/temp_file_implementation.txt", write text append
file write myfile "domestic (products imported (0) and produced in Portugal (1))" _n _n
file close myfile

file open  myfile using "${results_figures}/temp_file_reversal.txt", write text append
file write myfile "domestic (products imported (0) and produced in Portugal (1))" _n _n
file close myfile


forval i = 0(1)1 {

preserve

keep if domestic == `i'

// Keep the data around the event.
keep if five_day_number >= 17
keep if five_day_number <= 32

// Policy variable: 1 after the reference date.
replace treatment = 0 if five_day_number < 23

if ${weights} == 0 {

// Run the event-study regression.
xtevent index_23032023, policyvar(treatment) panelvar(id_sm) timevar(five_day_number) window(${window_low} ${window_high}) vce(cluster ${vce_cluster}) impute(nuchange) reghdfe trend(${trend}, method(gmm) saveoverlay)
	
}

if ${weights} == 1 {

// Run the event-study regression.
xtevent index_23032023 [pweight = weight], policyvar(treatment) panelvar(id_sm) timevar(five_day_number) window(${window_low} ${window_high}) vce(cluster ${vce_cluster}) impute(nuchange) reghdfe trend(${trend}, method(gmm) saveoverlay)
		
}

matrix results = r(delta)
matrix vcv = r(Vdelta)
local z = 1.96

file open  myfile using "${results_figures}/temp_file_implementation.txt", write text append

scalar coef = results[1,10]
scalar se   = sqrt(vcv[10,10])
generate coeff = coef
generate z = `z'
generate see = se
scalar lb = coeff - z * see
generate lbb = lb
scalar ub = coeff + z * see
generate ubb = ub
format %12.0g coeff lbb ubb
file write myfile "`i', " (lbb) ", " (coeff) ", " (ubb) _n _n
drop coeff z see lbb ubb

file close myfile

restore

preserve

keep if domestic == `i'

// Keep the data around the event.
keep if five_day_number >= 60
keep if five_day_number <= 83

// Policy variable: 1 after the reference date.
replace treatment = 0 if five_day_number < 66

if ${weights} == 0 {

// Run the event-study regression.
xtevent index_23032023, policyvar(treatment) panelvar(id_sm) timevar(five_day_number) window(${window_low} 16) vce(cluster ${vce_cluster}) impute(nuchange) reghdfe trend(${trend}, method(gmm) saveoverlay)
	
}

if ${weights} == 1 {

// Run the event-study regression.
xtevent index_23032023 [pweight = weight], policyvar(treatment) panelvar(id_sm) timevar(five_day_number) window(${window_low} 16) vce(cluster ${vce_cluster}) impute(nuchange) reghdfe trend(${trend}, method(gmm) saveoverlay)
		
}

matrix results = r(delta)
matrix vcv = r(Vdelta)
local z = 1.96

file open  myfile using "${results_figures}/temp_file_reversal.txt", write text append

scalar coef = results[1,19]
scalar se   = sqrt(vcv[19,19])
generate coeff = coef
generate z = `z'
generate see = se
scalar lb = coeff - z * see
generate lbb = lb
scalar ub = coeff + z * see
generate ubb = ub
format %12.0g coeff lbb ubb
file write myfile "`i', " (lbb) ", " (coeff) ", " (ubb) _n _n
drop coeff z see lbb ubb

file close myfile

restore

}

*********************************************************

// Load the cleaned dataset.
use "${path_work}/auxiliar/imputation_0_only_food.dta", clear

collapse (${aggregation}) ${variable} (first)  treatment weight big, by (id_sm five_day_number)

// Compute a price index with base in the day before policy announcement I.
local   base_date 22
egen    base_price_23032023 = total(${variable}/ (five_day_number == `base_date')), by(id_sm)
gen     index_23032023 = (${variable}/ base_price_23032023) * 100

xtset id_sm five_day_number

file open  myfile using "${results_figures}/temp_file_implementation.txt", write text append
file write myfile "big (products from small supermarkets (0) and big supermarkets (1))" _n _n
file close myfile

file open  myfile using "${results_figures}/temp_file_reversal.txt", write text append
file write myfile "big (products from small supermarkets (0) and big supermarkets (1))" _n _n
file close myfile


forval i = 0(1)1 {

preserve

keep if big == `i'

// Keep the data around the event.
keep if five_day_number >= 17
keep if five_day_number <= 32

// Policy variable: 1 after the reference date.
replace treatment = 0 if five_day_number < 23

if ${weights} == 0 {

// Run the event-study regression.
xtevent index_23032023, policyvar(treatment) panelvar(id_sm) timevar(five_day_number) window(${window_low} ${window_high}) vce(cluster ${vce_cluster}) impute(nuchange) reghdfe trend(${trend}, method(gmm) saveoverlay)
	
}

if ${weights} == 1 {

// Run the event-study regression.
xtevent index_23032023 [pweight = weight], policyvar(treatment) panelvar(id_sm) timevar(five_day_number) window(${window_low} ${window_high}) vce(cluster ${vce_cluster}) impute(nuchange) reghdfe trend(${trend}, method(gmm) saveoverlay)
		
}

matrix results = r(delta)
matrix vcv = r(Vdelta)
local z = 1.96

file open  myfile using "${results_figures}/temp_file_implementation.txt", write text append

scalar coef = results[1,10]
scalar se   = sqrt(vcv[10,10])
generate coeff = coef
generate z = `z'
generate see = se
scalar lb = coeff - z * see
generate lbb = lb
scalar ub = coeff + z * see
generate ubb = ub
format %12.0g coeff lbb ubb
file write myfile "`i', " (lbb) ", " (coeff) ", " (ubb) _n _n
drop coeff z see lbb ubb

file close myfile

restore

preserve

keep if big == `i'

// Keep the data around the event.
keep if five_day_number >= 60
keep if five_day_number <= 83

// Policy variable: 1 after the reference date.
replace treatment = 0 if five_day_number < 66

if ${weights} == 0 {

// Run the event-study regression.
xtevent index_23032023, policyvar(treatment) panelvar(id_sm) timevar(five_day_number) window(${window_low} 16) vce(cluster ${vce_cluster}) impute(nuchange) reghdfe trend(${trend}, method(gmm) saveoverlay)
	
}

if ${weights} == 1 {

// Run the event-study regression.
xtevent index_23032023 [pweight = weight], policyvar(treatment) panelvar(id_sm) timevar(five_day_number) window(${window_low} 16) vce(cluster ${vce_cluster}) impute(nuchange) reghdfe trend(${trend}, method(gmm) saveoverlay)
		
}

matrix results = r(delta)
matrix vcv = r(Vdelta)
local z = 1.96

file open  myfile using "${results_figures}/temp_file_reversal.txt", write text append

scalar coef = results[1,19]
scalar se   = sqrt(vcv[19,19])
generate coeff = coef
generate z = `z'
generate see = se
scalar lb = coeff - z * see
generate lbb = lb
scalar ub = coeff + z * see
generate ubb = ub
format %12.0g coeff lbb ubb
file write myfile "`i', " (lbb) ", " (coeff) ", " (ubb) _n _n
drop coeff z see lbb ubb

file close myfile

restore

}

*********************************************************

// Load the cleaned dataset.
use "${path_work}/auxiliar/imputation_0_only_food.dta", clear

collapse (${aggregation}) ${variable} (first)  treatment weight high_price, by (id_sm five_day_number)

// Compute a price index with base in the day before policy announcement I.
local   base_date 22
egen    base_price_23032023 = total(${variable}/ (five_day_number == `base_date')), by(id_sm)
gen     index_23032023 = (${variable}/ base_price_23032023) * 100

xtset id_sm five_day_number

file open  myfile using "${results_figures}/temp_file_implementation.txt", write text append
file write myfile "high_price (products with price below average (0) and above average (1))" _n _n
file close myfile

file open  myfile using "${results_figures}/temp_file_reversal.txt", write text append
file write myfile "high_price (products with price below average (0) and above average (1))" _n _n
file close myfile


forval i = 0(1)1 {

preserve

keep if high_price == `i'

// Keep the data around the event.
keep if five_day_number >= 17
keep if five_day_number <= 32

// Policy variable: 1 after the reference date.
replace treatment = 0 if five_day_number < 23

if ${weights} == 0 {

// Run the event-study regression.
xtevent index_23032023, policyvar(treatment) panelvar(id_sm) timevar(five_day_number) window(${window_low} ${window_high}) vce(cluster ${vce_cluster}) impute(nuchange) reghdfe trend(${trend}, method(gmm) saveoverlay)
	
}

if ${weights} == 1 {

// Run the event-study regression.
xtevent index_23032023 [pweight = weight], policyvar(treatment) panelvar(id_sm) timevar(five_day_number) window(${window_low} ${window_high}) vce(cluster ${vce_cluster}) impute(nuchange) reghdfe trend(${trend}, method(gmm) saveoverlay)
		
}

matrix results = r(delta)
matrix vcv = r(Vdelta)
local z = 1.96

file open  myfile using "${results_figures}/temp_file_implementation.txt", write text append

scalar coef = results[1,10]
scalar se   = sqrt(vcv[10,10])
generate coeff = coef
generate z = `z'
generate see = se
scalar lb = coeff - z * see
generate lbb = lb
scalar ub = coeff + z * see
generate ubb = ub
format %12.0g coeff lbb ubb
file write myfile "`i', " (lbb) ", " (coeff) ", " (ubb) _n _n
drop coeff z see lbb ubb

file close myfile

restore

preserve

keep if high_price == `i'

// Keep the data around the event.
keep if five_day_number >= 60
keep if five_day_number <= 83

// Policy variable: 1 after the reference date.
replace treatment = 0 if five_day_number < 66

if ${weights} == 0 {

// Run the event-study regression.
xtevent index_23032023, policyvar(treatment) panelvar(id_sm) timevar(five_day_number) window(${window_low} 16) vce(cluster ${vce_cluster}) impute(nuchange) reghdfe trend(${trend}, method(gmm) saveoverlay)
	
}

if ${weights} == 1 {

// Run the event-study regression.
xtevent index_23032023 [pweight = weight], policyvar(treatment) panelvar(id_sm) timevar(five_day_number) window(${window_low} 16) vce(cluster ${vce_cluster}) impute(nuchange) reghdfe trend(${trend}, method(gmm) saveoverlay)
		
}

matrix results = r(delta)
matrix vcv = r(Vdelta)
local z = 1.96

file open  myfile using "${results_figures}/temp_file_reversal.txt", write text append

scalar coef = results[1,19]
scalar se   = sqrt(vcv[19,19])
generate coeff = coef
generate z = `z'
generate see = se
scalar lb = coeff - z * see
generate lbb = lb
scalar ub = coeff + z * see
generate ubb = ub
format %12.0g coeff lbb ubb
file write myfile "`i', " (lbb) ", " (coeff) ", " (ubb) _n _n
drop coeff z see lbb ubb

file close myfile

restore

}

grstyle init
grstyle set plain, nogrid box noextend
grstyle graphsize x 10
grstyle graphsize y 14
grstyle color background white

// Load the data with the results.
import delimited "${results_figures}/temp_file_implementation.txt", stringcols(1) numericcols(2 3 4) varnames(nonames) clear

drop if v1 == "lower_ci" | v1 == "ecoicops"
drop if v1 == "total" & v2 ==.

scalar implementation_lower_total = v2[1]
local  implementation_lower_total = implementation_lower_total

scalar implementation_total = v3[1]
local  implementation_total = implementation_total

scalar implementation_upper_total = v4[1]
local  implementation_upper_total = implementation_upper_total

drop if v1 == "total"

gen nr = _n

order nr

replace v1 = "Bread and cereals"      if v1 == "0111"
replace v1 = "Meat"                   if v1 == "0112"
replace v1 = "Fish and seafood"       if v1 == "0113"
replace v1 = "Milk, cheese and eggs"  if v1 == "0114"
replace v1 = "Oils and fats"          if v1 == "0115"
replace v1 = "Fruits"                 if v1 == "0116"
replace v1 = "Vegetables"             if v1 == "0117"

replace nr = 1 if nr == 7  & v1 == "Vegetables"
replace nr = 2 if nr == 6  & v1 == "Fruits"
replace nr = 3 if nr == 4  & v1 == "Milk, cheese and eggs"
replace nr = 4 if nr == 1  & v1 == "Bread and cereals"
replace nr = 5 if nr == 2  & v1 == "Meat"
replace nr = 6 if nr == 3  & v1 == "Fish and seafood"
replace nr = 7 if nr == 5  & v1 == "Oils and fats"

replace v1 = " " if nr == 8

replace v1 = "Trademark brand" if nr == 9
replace v1 = "While-label brand" if nr == 10

replace v1 = "  " if nr == 11

replace v1 = "Imported items" if nr == 12
replace v1 = "Domestic items" if nr == 13

replace v1 = "   " if nr == 14

replace v1 = "Small supermarkets" if nr == 15
replace v1 = "Big supermarkets" if nr == 16

replace nr = 15 if nr == 16 & v1 == "Big supermarkets"
replace nr = 16 if nr == 15 & v1 == "Small supermarkets"

replace v1 = "    " if nr == 17

replace v1 = "Low price items" if nr == 18
replace v1 = "High price items" if nr == 19

replace nr = 18 if nr == 19 & v1 == "High price items"
replace nr = 19 if nr == 18 & v1 == "Low price items"

sort nr

rename v1 name_eng
rename v2 implementation_lower
rename v3 implementation
rename v4 implementation_upper

// Make the plot with the results.
graph dot (asis) implementation (asis) implementation_lower (asis) implementation_upper, ///
	over(name_eng, sort(nr)  axis(lcolor(none))   ///
	label(labsize(medsmall))) xalternate ///
	marker(1, mcolor("dknavy") msymbol(circle) msize(small)) ///
	marker(2, mcolor("black") msymbol(pipe)) ///
	marker(3, mcolor("black") msymbol(pipe)) ///
	ytitle("Implementation") graphregion(fcolor(white)) ///
	yscale(lcolor(black) lwidth(none) line) ylabel(-15(5)5) ///
	yline(`implementation_lower_total', lstyle(solid) lcolor(gs12)) ///
	yline(`implementation_total', lstyle(solid) lcolor(dknavy)) ///
	yline(`implementation_upper_total', lstyle(solid) lcolor(gs12)) ///
	legend(off) allcategories
	
// Export the graph.
graph export "${results_figures}/figure_7a.png", as(png) replace width(2400) height(1372)

*********************************************************

// Load the data with the results.
import delimited "${results_figures}/temp_file_reversal.txt", stringcols(1) numericcols(2 3 4) varnames(nonames) clear

drop if v1 == "lower_ci" | v1 == "ecoicops"
drop if v1 == "total" & v2 ==.

scalar reversal_lower_total = v2[1]
local  reversal_lower_total = reversal_lower_total

scalar reversal_total = v3[1]
local  reversal_total = reversal_total

scalar reversal_upper_total = v4[1]
local  reversal_upper_total = reversal_upper_total

drop if v1 == "total"

gen nr = _n

order nr

replace v1 = "Bread and cereals"      if v1 == "0111"
replace v1 = "Meat"                   if v1 == "0112"
replace v1 = "Fish and seafood"       if v1 == "0113"
replace v1 = "Milk, cheese and eggs"  if v1 == "0114"
replace v1 = "Oils and fats"          if v1 == "0115"
replace v1 = "Fruits"                 if v1 == "0116"
replace v1 = "Vegetables"             if v1 == "0117"

replace nr = 1 if nr == 7  & v1 == "Vegetables"
replace nr = 2 if nr == 6  & v1 == "Fruits"
replace nr = 3 if nr == 4  & v1 == "Milk, cheese and eggs"
replace nr = 4 if nr == 1  & v1 == "Bread and cereals"
replace nr = 5 if nr == 2  & v1 == "Meat"
replace nr = 6 if nr == 3  & v1 == "Fish and seafood"
replace nr = 7 if nr == 5  & v1 == "Oils and fats"

replace v1 = " " if nr == 8

replace v1 = "Trademark brand" if nr == 9
replace v1 = "While-label brand" if nr == 10

replace v1 = "  " if nr == 11

replace v1 = "Imported items" if nr == 12
replace v1 = "Domestic items" if nr == 13

replace v1 = "   " if nr == 14

replace v1 = "Small supermarkets" if nr == 15
replace v1 = "Big supermarkets" if nr == 16

replace nr = 15 if nr == 16 & v1 == "Big supermarkets"
replace nr = 16 if nr == 15 & v1 == "Small supermarkets"

replace v1 = "    " if nr == 17

replace v1 = "Low price items" if nr == 18
replace v1 = "High price items" if nr == 19

replace nr = 18 if nr == 19 & v1 == "High price items"
replace nr = 19 if nr == 18 & v1 == "Low price items"

sort nr

rename v1 name_eng
rename v2 reversal_lower
rename v3 reversal
rename v4 reversal_upper

// Make the plot with the results.
graph dot (asis) reversal (asis) reversal_lower (asis) reversal_upper, ///
	over(name_eng, sort(nr) axis(lcolor(none)) ///
	label(labsize(medsmall))) xalternate ///
	marker(1, mcolor("orange") msymbol(circle) msize(small)) ///
	marker(2, mcolor("black") msymbol(pipe)) ///
	marker(3, mcolor("black") msymbol(pipe)) ///
	ytitle("Reversal") graphregion(fcolor(white)) ///
	yscale(lcolor(black) lwidth(none) line) ylabel(-40(20)40) ///
	yline(`reversal_lower_total', lstyle(solid) lcolor(gs12)) ///
	yline(`reversal_total', lstyle(solid) lcolor("orange")) ///
	yline(`reversal_upper_total', lstyle(solid) lcolor(gs12)) ///
	legend(off) allcategories

// Export the graph.
graph export "${results_figures}/figure_7b.png", as(png) replace width(2400) height(1372)

*********************************************************


** FIGURE A9: Heterogeneous Net Treatment Effects
*********************************************************

// Load the cleaned dataset.
use "${path_work}/auxiliar/imputation_0_only_food.dta", clear

collapse (last) ${variable} (first) ecoicop4 treatment weight, by (id_sm month_year)

// Compute a price index with base in the day before policy announcement I.
local   base_date 2023m2
egen    base_price_23032023 = total(${variable}/ (month_year == tm(`base_date'))), by(id_sm)
gen     index_23032023 = (${variable}/ base_price_23032023) * 100

xtset id_sm month_year

file open  myfile using "${results_figures}/temp_file_net_effect.txt", write text replace
file write myfile "lower_ci, coefficient, upper_ci" _n _n
file close myfile

preserve

// Policy variable: 1 after the reference date.
replace treatment = 0 if month_year < tm(2023m4)

// Run the event-study regression.
xtevent index_23032023 [pweight = weight], policyvar(treatment) panelvar(id_sm) timevar(month_year) window(-2 9) vce(cluster ${vce_cluster}) impute(nuchange) reghdfe

matrix results = r(delta)
matrix vcv = r(Vdelta)
local z = 1.96

file open  myfile using "${results_figures}/temp_file_net_effect.txt", write text append

scalar coef = results[1,12]
scalar se   = sqrt(vcv[12,12])
generate coeff = coef
generate z = `z'
generate see = se
scalar lb = coeff - z * see
generate lbb = lb
scalar ub = coeff + z * see
generate ubb = ub
format %12.0g coeff lbb ubb
file write myfile "total, " (lbb) ", " (coeff) ", " (ubb) _n _n
drop coeff z see lbb ubb

file close myfile
	
restore

*********************************************************

/*

// Specify a list of dataset names
local coicops "01111 01113 01116 01121 01122 01124 01131 01132 01141 01142 01144 01145 01146 01147 01151 01153 01161 01171 01172 01174"

file open  myfile using "${results_figures}/temp_file_net_effect.txt", write text append
file write myfile "lower_ci, coefficient, upper_ci" _n _n "ecoicops" _n _n
file close myfile

foreach coicop in `coicops' {

preserve

drop if (treatment == 1 & ecoicop != `coicop')

// Policy variable: 1 after the reference date.
replace treatment = 0 if month_year < tm(2023m4)

// Run the event-study regression.
xtevent index_23032023 [pweight = weight], policyvar(treatment) panelvar(id_sm) timevar(month_year) window(-2 9) vce(cluster ${vce_cluster}) impute(nuchange) reghdfe

matrix results = r(delta)
matrix vcv = r(Vdelta)
local z = 1.96

file open  myfile using "${results_figures}/temp_file_net_effect.txt", write text append

scalar coef = results[1,12]
scalar se   = sqrt(vcv[12,12])
generate coeff = coef
generate z = `z'
generate see = se
scalar lb = coeff - z * see
generate lbb = lb
scalar ub = coeff + z * see
generate ubb = ub
format %12.0g coeff lbb ubb
file write myfile "`coicop', " (lbb) ", " (coeff) ", " (ubb) _n _n
drop coeff z see lbb ubb

file close myfile
	
restore

}

*/

// Specify a list of dataset names
local coicops "0111 0112 0113 0114 0115 0116 0117"

file open  myfile using "${results_figures}/temp_file_net_effect.txt", write text append
file write myfile "ecoicops" _n _n
file close myfile

foreach coicop in `coicops' {

preserve

drop if (treatment == 1 & ecoicop4 != `coicop')

// Policy variable: 1 after the reference date.
replace treatment = 0 if month_year < tm(2023m4)

// Run the event-study regression.
xtevent index_23032023 [pweight = weight], policyvar(treatment) panelvar(id_sm) timevar(month_year) window(-2 9) vce(cluster ${vce_cluster}) impute(nuchange) reghdfe

matrix results = r(delta)
matrix vcv = r(Vdelta)
local z = 1.96

file open  myfile using "${results_figures}/temp_file_net_effect.txt", write text append

scalar coef = results[1,12]
scalar se   = sqrt(vcv[12,12])
generate coeff = coef
generate z = `z'
generate see = se
scalar lb = coeff - z * see
generate lbb = lb
scalar ub = coeff + z * see
generate ubb = ub
format %12.0g coeff lbb ubb
file write myfile "`coicop', " (lbb) ", " (coeff) ", " (ubb) _n _n
drop coeff z see lbb ubb

file close myfile
	
restore

}



*********************************************************

// Load the cleaned dataset.
use "${path_work}/auxiliar/imputation_0_only_food.dta", clear

collapse (last) ${variable} (first) white_label treatment weight, by (id_sm month_year)

// Compute a price index with base in the day before policy announcement I.
local   base_date 2023m2
egen    base_price_23032023 = total(${variable}/ (month_year == tm(`base_date'))), by(id_sm)
gen     index_23032023 = (${variable}/ base_price_23032023) * 100

xtset id_sm month_year

file open  myfile using "${results_figures}/temp_file_net_effect.txt", write text append
file write myfile "white_label (trademark-labels (0) and white-labels (1))" _n _n
file close myfile

forval i = 0(1)1 {

preserve

keep if white_label == `i'

// Policy variable: 1 after the reference date.
replace treatment = 0 if month_year < tm(2023m4)

// Run the event-study regression.
xtevent index_23032023 [pweight = weight], policyvar(treatment) panelvar(id_sm) timevar(month_year) window(-2 9) vce(cluster ${vce_cluster}) impute(nuchange) reghdfe

matrix results = r(delta)
matrix vcv = r(Vdelta)
local z = 1.96

file open  myfile using "${results_figures}/temp_file_net_effect.txt", write text append

scalar coef = results[1,12]
scalar se   = sqrt(vcv[12,12])
generate coeff = coef
generate z = `z'
generate see = se
scalar lb = coeff - z * see
generate lbb = lb
scalar ub = coeff + z * see
generate ubb = ub
format %12.0g coeff lbb ubb
file write myfile "`i', " (lbb) ", " (coeff) ", " (ubb) _n _n
drop coeff z see lbb ubb

file close myfile

restore

}

*********************************************************

// Load the cleaned dataset.
use "${path_work}/auxiliar/imputation_0_only_food.dta", clear

collapse (last) ${variable} (first) domestic treatment weight, by (id_sm month_year)

// Compute a price index with base in the day before policy announcement I.
local   base_date 2023m2
egen    base_price_23032023 = total(${variable}/ (month_year == tm(`base_date'))), by(id_sm)
gen     index_23032023 = (${variable}/ base_price_23032023) * 100

xtset id_sm month_year

file open  myfile using "${results_figures}/temp_file_net_effect.txt", write text append
file write myfile "domestic (products imported (0) and produced in Portugal (1))" _n _n
file close myfile

forval i = 0(1)1 {

preserve

keep if domestic == `i'

// Policy variable: 1 after the reference date.
replace treatment = 0 if month_year < tm(2023m4)

// Run the event-study regression.
xtevent index_23032023 [pweight = weight], policyvar(treatment) panelvar(id_sm) timevar(month_year) window(-2 9) vce(cluster ${vce_cluster}) impute(nuchange) reghdfe

matrix results = r(delta)
matrix vcv = r(Vdelta)
local z = 1.96

file open  myfile using "${results_figures}/temp_file_net_effect.txt", write text append

scalar coef = results[1,12]
scalar se   = sqrt(vcv[12,12])
generate coeff = coef
generate z = `z'
generate see = se
scalar lb = coeff - z * see
generate lbb = lb
scalar ub = coeff + z * see
generate ubb = ub
format %12.0g coeff lbb ubb
file write myfile "`i', " (lbb) ", " (coeff) ", " (ubb) _n _n
drop coeff z see lbb ubb

file close myfile

restore

}

*********************************************************

// Load the cleaned dataset.
use "${path_work}/auxiliar/imputation_0_only_food.dta", clear

collapse (last) ${variable} (first) big treatment weight, by (id_sm month_year)

// Compute a price index with base in the day before policy announcement I.
local   base_date 2023m2
egen    base_price_23032023 = total(${variable}/ (month_year == tm(`base_date'))), by(id_sm)
gen     index_23032023 = (${variable}/ base_price_23032023) * 100

xtset id_sm month_year

file open  myfile using "${results_figures}/temp_file_net_effect.txt", write text append
file write myfile "big (products from small supermarkets (0) and big supermarkets (1))" _n _n
file close myfile

forval i = 0(1)1 {

preserve

keep if big == `i'

// Policy variable: 1 after the reference date.
replace treatment = 0 if month_year < tm(2023m4)

// Run the event-study regression.
xtevent index_23032023 [pweight = weight], policyvar(treatment) panelvar(id_sm) timevar(month_year) window(-2 9) vce(cluster ${vce_cluster}) impute(nuchange) reghdfe

matrix results = r(delta)
matrix vcv = r(Vdelta)
local z = 1.96

file open  myfile using "${results_figures}/temp_file_net_effect.txt", write text append

scalar coef = results[1,12]
scalar se   = sqrt(vcv[12,12])
generate coeff = coef
generate z = `z'
generate see = se
scalar lb = coeff - z * see
generate lbb = lb
scalar ub = coeff + z * see
generate ubb = ub
format %12.0g coeff lbb ubb
file write myfile "`i', " (lbb) ", " (coeff) ", " (ubb) _n _n
drop coeff z see lbb ubb

file close myfile

restore

}

*********************************************************

// Load the cleaned dataset.
use "${path_work}/auxiliar/imputation_0_only_food.dta", clear

collapse (last) ${variable} (first) high_price treatment weight, by (id_sm month_year)

// Compute a price index with base in the day before policy announcement I.
local   base_date 2023m2
egen    base_price_23032023 = total(${variable}/ (month_year == tm(`base_date'))), by(id_sm)
gen     index_23032023 = (${variable}/ base_price_23032023) * 100

xtset id_sm month_year

file open  myfile using "${results_figures}/temp_file_net_effect.txt", write text append
file write myfile "high_price (products with price below average (0) and above average (1))" _n _n
file close myfile

forval i = 0(1)1 {

preserve

keep if high_price == `i'

// Policy variable: 1 after the reference date.
replace treatment = 0 if month_year < tm(2023m4)

// Run the event-study regression.
xtevent index_23032023 [pweight = weight], policyvar(treatment) panelvar(id_sm) timevar(month_year) window(-2 9) vce(cluster ${vce_cluster}) impute(nuchange) reghdfe

matrix results = r(delta)
matrix vcv = r(Vdelta)
local z = 1.96

file open  myfile using "${results_figures}/temp_file_net_effect.txt", write text append

scalar coef = results[1,12]
scalar se   = sqrt(vcv[12,12])
generate coeff = coef
generate z = `z'
generate see = se
scalar lb = coeff - z * see
generate lbb = lb
scalar ub = coeff + z * see
generate ubb = ub
format %12.0g coeff lbb ubb
file write myfile "`i', " (lbb) ", " (coeff) ", " (ubb) _n _n
drop coeff z see lbb ubb

file close myfile

restore

}

*********************************************************

grstyle init
grstyle set plain, nogrid box noextend
grstyle graphsize x 14
grstyle graphsize y 16
grstyle color background white

// Load the data with the results.
import delimited "${results_figures}/temp_file_net_effect.txt", stringcols(1) numericcols(2 3 4) varnames(nonames) clear

drop if v1 == "lower_ci" | v1 == "ecoicops"
drop if v1 == "total" & v2 ==.

scalar total_net_effect_lower_total = v2[1]
local  total_net_effect_lower_total = total_net_effect_lower_total

scalar total_net_effect_total = v3[1]
local  total_net_effect_total = total_net_effect_total

scalar total_net_effect_upper_total = v4[1]
local  total_net_effect_upper_total = total_net_effect_upper_total

drop if v1 == "total"

gen nr = _n

order nr

replace v1 = "Bread and cereals"      if v1 == "0111"
replace v1 = "Meat"                   if v1 == "0112"
replace v1 = "Fish and seafood"       if v1 == "0113"
replace v1 = "Milk, cheese and eggs"  if v1 == "0114"
replace v1 = "Oils and fats"          if v1 == "0115"
replace v1 = "Fruits"                 if v1 == "0116"
replace v1 = "Vegetables"             if v1 == "0117"

replace nr = 1 if nr == 7  & v1 == "Vegetables"
replace nr = 2 if nr == 6  & v1 == "Fruits"
replace nr = 3 if nr == 4  & v1 == "Milk, cheese and eggs"
replace nr = 4 if nr == 1  & v1 == "Bread and cereals"
replace nr = 5 if nr == 2  & v1 == "Meat"
replace nr = 6 if nr == 3  & v1 == "Fish and seafood"
replace nr = 7 if nr == 5  & v1 == "Oils and fats"

replace v1 = " " if nr == 8

replace v1 = "Trademark brand" if nr == 9
replace v1 = "While-label brand" if nr == 10

replace v1 = "  " if nr == 11

replace v1 = "Imported items" if nr == 12
replace v1 = "Domestic items" if nr == 13

replace v1 = "   " if nr == 14

replace v1 = "Small supermarkets" if nr == 15
replace v1 = "Big supermarkets" if nr == 16

replace nr = 15 if nr == 16 & v1 == "Big supermarkets"
replace nr = 16 if nr == 15 & v1 == "Small supermarkets"

replace v1 = "    " if nr == 17

replace v1 = "Low price items" if nr == 18
replace v1 = "High price items" if nr == 19

replace nr = 18 if nr == 19 & v1 == "High price items"
replace nr = 19 if nr == 18 & v1 == "Low price items"

sort nr

rename v1 name_eng
rename v2 total_net_lower
rename v3 total_net
rename v4 total_net_upper

// Make the plot with the results.
graph dot (asis) total_net (asis) total_net_lower (asis) total_net_upper, ///
	over(name_eng, sort(nr) axis(lcolor(none)) ///
	label(labsize(small))) xalternate ///
	marker(1, mcolor("green") msymbol(circle) msize(small)) ///
	marker(2, mcolor("black") msymbol(pipe)) ///
	marker(3, mcolor("black") msymbol(pipe)) ///
	ytitle("Total Net Effect") graphregion(fcolor(white)) ///
	yscale(lcolor(black) lwidth(none) line) ///
	yline(`total_net_effect_lower_total', lstyle(solid) lcolor(gs12)) ///
	yline(`total_net_effect_total', lstyle(solid) lcolor("green")) ///
	yline(`total_net_effect_upper_total', lstyle(solid) lcolor(gs12)) ///
	legend(off) allcategories

// Export the graph.
graph export "${results_figures}/figure_a10.png", as(png) replace

*********************************************************

grstyle init
grstyle set plain, nogrid box noextend
grstyle graphsize x 14
grstyle graphsize y 8
grstyle color background white

*********************************************************

erase "${results_figures}/temp_file_implementation.txt"
erase "${results_figures}/temp_file_reversal.txt"
erase "${results_figures}/temp_file_net_effect.txt"

if ${robustness} == 0 {

erase "${path_work}/auxiliar/combined_dataset.dta"
erase "${path_work}/auxiliar/imputation_0_only_food.dta"
erase "${path_work}/auxiliar/imputation_0_reduced_tax_coicop.dta"
erase "${path_work}/auxiliar/imputation_0_all_products.dta"
erase "${path_work}/auxiliar/merged_zero_vat_list.dta"

}

*********************************************************
